#!/bin/bash 

CUR=`pwd`
PYTHON=python3


PYLOG=${CUR}/pylog/pylog.py
MONITOR=${CUR}/test_main/monitor_main.py


CMD_MONITOR="monitor"
CMD_PYLOG="pylog"



SUB_USAGE="usage: $0 ${CMD_PYLOG}/${CMD_MONITOR}"
function do_run {
    echo "do_run $1"

    if [ $# -le 0 ] 
    then
        echo "error **********************" 
        echo ${SUB_USAGE}
        return
    fi

    case $1 in 
        ${CMD_PYLOG}) 
            #source ${ACTIVEATE} 
           ${PYTHON} ${PYLOG} &
        ;;
        ${CMD_MONITOR})
           #source ${ACTIVEATE} 
           ${PYTHON} ${MONITOR} &
        ;;
        *)
            echo "err ${SUB_USAGE}"
        ;;
    esac
}

function do_stop {
    echo "do_stop $1"

    if [ $# -le 0 ] 
    then
        echo "error **********************" 
        echo ${SUB_USAGE}
        return
    fi

    case $1 in 
        ${CMD_PYLOG})
            ps aux|grep ${PYLOG} |grep -v grep |awk '{print $2}'|xargs kill -9 >/dev/null 2>&1
        ;;
        ${CMD_MONITOR})
            ps aux|grep ${MONITOR} |grep -v grep |awk '{print $2}'|xargs kill -9 >/dev/null 2>&1
        ;;
        *)
            echo "err ${SUB_USAGE}"
        ;;
    esac

}


USAGE="usage: $0 run/stop"
function main {
   if [ $# -le 0 ] 
   then
       echo "error **********************" 
       echo ${USAGE}
       return
   fi

   case $1 in 
    "run")
        do_stop $2
        do_run $2
        return
    ;;
    "stop")
        do_stop $2
        return
    ;;
    *)
       echo "error **********************" 
       echo ${USAGE}
       return
    ;;
   esac
}


main $*

exit 0
